# Logging functions
# 
###############################################################################


#' Submits a message to a log file
#' 
#' A message together with a date stamp and severity is written to a log file. The log file is read/set 
#' using getLogFileLocation()/setLogFileLocation() and defaults to "log.txt"  
#' @param text The text of the message.
#' @param type The type or severity of the message. This can be one of the following:
#' \describe{
#' \item{Note}{A notification to the user}
#' \item{Warning}{A warning to the user, this can be used to highlight unusual pipeline setups, which are likely to be 
#' errors}
#' \item{Error}{This is used, if an error occurred during pipeline setup or execution.}}
#' @export
#' @author Bastian R. Angermann
#' @examples \dontrun{submitToLog("This is an example","Note")}
submitToLog <- function(text,type)
{
    validTypes <- c("Note", "Warning", "Error")
    if (! (type %in% validTypes) ) 
        submitToLog("Invalid message code submitted to log. This is a bug. Please contact the maintainer.", "Error")
    else {
          
        theCall <- deparse(sys.call( sys.parent()) [[1]])
        message <- paste(paste(date(),theCall,text, type, sep="\t"), "\n", sep="")
        catchedMessage <- warnings()
        if (!is.null(catchedMessage))
            message <- paste(message,"\tAdditionally R issued the following warning:\n",catchedMessage$message,"\n")

        cat(file=getLogFileLocation(), message, append=TRUE)
    }
    if (type =="Error")
    {    
        cat(paste(paste(date(),text,type,sep="\t"),"\n",sep=""))
        stop(paste("Error during pipeline Execution see log:",getLogFileLocation() ))
    }
}
